#ifndef SRC_BASE_FILE_UTIL_H
#define SRC_BASE_FILE_UTIL_H

#include "base/macro_def.h"
#include <cstdio>

inline bool FileExist(const std::string& path) {
  if (path.empty()) return false;

  struct stat st = { 0 };
  int ret = ::stat(path.c_str(), &st);
  return ret == 0;
}

inline bool ReName(const std::string& o, const std::string& n) {
  if (o.empty() || n.empty()) return false;

  return ::rename(o.c_str(), n.c_str()) == 0;
}

inline int32_t FileSize(const std::string& path) {
  if (path.empty()) return -1;

  struct stat st = { 0 };
  int ret = ::stat(path.c_str(), &st);
  if (ret == -1) return -1;

  return st.st_size;
}

inline bool FileEmpty(const std::string& path) {
  return FileSize(path) == 0;
}

inline void DeleteFile(const std::string& path) {
  if (path.empty()) return;
  ::unlink(path.c_str());
}

#endif  // SRC_BASE_FILE_UTIL_H
